import { defineStore, acceptHMRUpdate } from 'pinia'
import { Gitlab } from '@gitbeaker/rest';
import type { ExpandedUserSchema } from '@gitbeaker/rest';
import type { BaseResourceOptions, ResourceOptions, RequesterType, ResponseBodyTypes, DefaultRequestOptions, FormattedResponse } from '@gitbeaker/requester-utils';
import axios from 'axios';



export const useConfigStore = defineStore('config', {
  persist: {
    paths: ['gitlibConfig']
  },
  state: () => {
    return {
      gitlabConfig: ({
        // 服务地址
        host: 'http://10.101.37.217',
        // 服务令牌
        token: 'xMyjD_gnvAm9tDdraRMy',
        queryTimeout: 15_000
      } as BaseResourceOptions<false>),
      currentUser: null as ExpandedUserSchema | null
    }
  },
  getters: {
    getGitLabApi(state) {
      return new Gitlab({
        ...state.gitlabConfig,
        // requesterFn(resourceOptions: ResourceOptions): RequesterType {
        //   const http = axios.create({
        //     baseURL: resourceOptions.url,
        //     headers: resourceOptions.headers
        //   });
        //   http.interceptors.response.use(resp => {
        //     return {
        //       body: resp.data,
        //       headers: resp.headers,
        //       status: resp.status,
        //     } as any
        //   })

        //   return {
        //     async get<T extends ResponseBodyTypes>(endpoint: string, options?: DefaultRequestOptions): Promise<FormattedResponse<T>> {
        //       const authHeaders: ResourceOptions['headers'] = {}
        //       for(let key in resourceOptions.authHeaders) {
        //         authHeaders[key] = await resourceOptions.authHeaders[key]()
        //       }
        //       return http.get(endpoint, { data: options?.body, params: options?.searchParams, headers: authHeaders })
        //     },
        //     async post(endpoint, options) {
        //       const authHeaders: ResourceOptions['headers'] = {}
        //       for(let key in resourceOptions.authHeaders) {
        //         authHeaders[key] = await resourceOptions.authHeaders[key]()
        //       }
        //       return http.post(endpoint, { data: options?.body, params: options?.searchParams })
        //     },
        //     async patch(endpoint, options) {
        //       const authHeaders: ResourceOptions['headers'] = {}
        //       for(let key in resourceOptions.authHeaders) {
        //         authHeaders[key] = await resourceOptions.authHeaders[key]()
        //       }
        //       return http.patch(endpoint, { data: options?.body, params: options?.searchParams })
        //     },
        //     async delete(endpoint, options) {
        //       const authHeaders: ResourceOptions['headers'] = {}
        //       for(let key in resourceOptions.authHeaders) {
        //         authHeaders[key] = await resourceOptions.authHeaders[key]()
        //       }
        //       return http.patch(endpoint, { data: options?.body, params: options?.searchParams })
        //     },
        //     async put(endpoint, options) {
        //       const authHeaders: ResourceOptions['headers'] = {}
        //       for(let key in resourceOptions.authHeaders) {
        //         authHeaders[key] = await resourceOptions.authHeaders[key]()
        //       }
        //       return http.patch(endpoint, { data: options?.body, params: options?.searchParams })
        //     },
        //   }
        // }
      })
    }
  },
  actions: {
    async getCurrentUser() {
      this.currentUser = await this.getGitLabApi.Users.showCurrentUser();
      return this.currentUser;
    },
    logout() {
      this.currentUser = null;
    }
  }
})


// 确保传递正确的 store 声明
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useConfigStore, import.meta.hot))
}
